library ieee;
use ieee.std_logic_1164.all;


entity controller is port (

	clk_in :in std_logic;
	clk_out :out std_logic;
		
	strobe: out std_logic;
	
	PAD_INPUT : in std_logic;
		
	A: out std_logic;
	b: out std_logic;
	sele: out std_logic;
	start: out std_logic;
	u: out std_logic;
	d: out std_logic;
	l: out std_logic;
	r: out std_logic

);
end entity;

architecture arch of controller is

signal count : std_logic_vector(3 downto 0);
signal i			: integer range 0 to 648;
signal clk, preGPI : std_logic;
signal t_u : std_logic;
	signal t_A:  std_logic;
	signal t_b:  std_logic;
	signal t_sele:  std_logic;
	signal t_start:  std_logic;
	signal t_d:  std_logic;
	signal t_l:  std_logic;
	signal t_r:  std_logic;
begin

strobe <= preGPI;


process (preGPI,clk, count)
begin
if( preGPI = '1' or count = "0010") then clk_out <= '0';
else clk_out <=clk;
end if;
end process;

process (clk_in, preGPI)
	begin
	
	if(rising_edge(clk_in)) then
		if(i < 325 ) then
			i <= i+1;
			clk <= '0';
		elsif (i = 648) then
			i <= 0;
			clk <= '1';
		else
			i <= i + 1;
			clk <= '1';
		end if;	
	end if;
	end process;


process (clk, count)
	begin
		if(count = "0000" and rising_edge(clk)) then
			preGPI <= '1';
			count <= "0001";
			t_A <= PAD_INPUT;

		elsif(count = "0001" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "0010";
			t_A <= PAD_INPUT;

		elsif(count = "0010" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "0011";
			t_A <= PAD_INPUT;

		elsif(count = "0011" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "0100";

			t_B <= PAD_INPUT;
	
		elsif(count = "0100" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "0101";

			t_sele <= PAD_INPUT;

		elsif(count = "0101" and rising_edge(clk)) then
			preGPI  <= '0';
			count <= "0110";

			t_start <= PAD_INPUT;

		elsif(count = "0110" and rising_edge(clk)) then
			preGPI  <= '0';
			count <= "0111";

			t_u <= PAD_INPUT;

		elsif(count = "0111" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "1000";
			t_d <= PAD_INPUT;
		elsif(count = "1000" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "1001";
			t_l <= PAD_INPUT;

		elsif(count = "1001" and rising_edge(clk)) then
			preGPI <= '0';
			count <= "1010";
			t_r <= PAD_INPUT;
		elsif(rising_edge(clk)) then
			preGPI  <= '0';
			count <= "0000";
	
		end if;
	end process;
	u <= t_u;
	A <= t_A;
	B <= t_B;
	u <= t_u;
	l <= t_l;
	r <= t_r;
	d <= t_d;
	start <= t_start;
	sele <= t_sele;
	
end;